import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";

const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        leftMenuList: [], //一级分类的数据
        reclassMenuList: [], //二级分类标题的数据
        secondaryList: [], //二级分类下的商品数据
        active: 0,
        currentIndex: 0, //默认先选中第一个
        isFavorite: false, //默认先不点亮销量上下按钮
        isPrice: false, // 默认先不点亮价格上下按钮
        pitchId: 1, //默认显示销量
        condition_one: true, //默认销量可以点击
        condition_two: false, //默认价格不点击
        index: "",
        deptLowId: 1, //一级分类的id
    },
    //获取二级分类要传的数据
    secondary: {
        page: 1, //页数
        size: 10, //条数
        goodName: "", //商品的名称
        classifyOneId: 1, //一级的id
        classifyTwoId: "", //二级分类的id
        salesOrderType: 2, //销量排序 1升序2降序
        priceOrderType: "", //价格排序 1升序2降序
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options);
        this.setData({
            wh: app.globalData.sysInfo ? app.globalData.sysInfo.windowHeight : "",
        })
        if (options.type) { //这里面的id是通过首页点击的
            this.secondary.classifyOneId = +options.type + 1; //从首页点击的分类进入分类页面 需要的id
            this.setData({
                currentIndex: +options.type, //赋值上个页面点击选中的下标

                deptLowId: +options.type + 1 //赋值一级分类的id  
            })
        }
        this.separatedPost(); //一级分类的数据
        this.secondaryPost(); //二级标题下的数据
        this.primaryPost(); //一级分类的下的所有商品数据
        this.reclassifyPost(); //二级分类
    },
    // 点击左侧菜单的点击事件
    handleItemTap(e) {
        console.log(e);
        const { index } = e.currentTarget.dataset;
        //得到一级分类的id
        const id = e.currentTarget.id;
        this.setData({
            currentIndex: index,
            deptLowId: +id
        });
        this.secondary.page = 1;
        this.secondary.classifyOneId = id; //一级分类的id
        this.secondary.classifyTwoId = null;
        this.primaryPost(); //一级分类的下的所有商品数据
        this.reclassifyPost(); //二级分类的标题数据
    },
    //一级分类的数据
    async separatedPost() {
        let res = await request({ url: "Classify/findUserAll", data: { pid: 0 } });
        console.log(res, "一级分类");
        if (res.code == 200) {
            this.setData({
                leftMenuList: res.data, //赋值一级分类
                active: 0,
            });
        }
    },

    // 一级分类下的所有数据
    async primaryPost() {
        let res = await request({
            url: "Goods/findUserAll",
            data: this.secondary,
        });
        console.log(res, "一级分类的所有数据");
        // 获取 总条数
        const total = res.data.total;
        // 计算总页数
        this.totalPages = Math.ceil(total / this.secondary.size);
        if (res.code == 200) {
            this.setData({
                secondaryList: res.data.records,
            });
        }
    },
    // 一级分类下的所有数据
    async primaryPostText() {
        let res = await request({
            url: "Goods/findUserAll",
            data: this.secondary,
        });
        console.log(res, "一级分类的所有数据");
        // 获取 总条数
        const total = res.data.total;
        // 计算总页数
        this.totalPages = Math.ceil(total / this.secondary.size);
        if (res.code == 200) {
            this.setData({
                secondaryList: [...this.data.secondaryList, ...res.data.records],
            });
        }
    },
    // 二级标题数据
    async reclassifyPost() {
        let res = await request({
            url: "Classify/findUserAll",
            data: { pid: this.data.deptLowId },
        });
        console.log(res, "二级分类的标题");
        if (res.code == 200) {
            this.setData({
                reclassMenuList: res.data, //赋值二级分类
                active: 0,
            });
        }
    },
    //二级标题的按钮
    secondaryBtn(e) {
        console.log(e);
        this.setData({
            index: e.detail.index,
        });
        this.secondary.page = 1;
        this.secondary.classifyTwoId = e.target.dataset.id; //赋值二级分类的id
        this.secondaryPost(); //二级标题下的数据
    },
    // 点击二级标题获取二级标题下的数据  
    async secondaryPost() {
        let res = await request({
            url: "Goods/findUserAll",
            data: this.secondary,
        });
        console.log(res, "点击二级分类的商品");

        // 获取 总条数
        const total = res.data.total;
        // 计算总页数
        this.totalPages = Math.ceil(total / this.secondary.size);
        if (res.code == 200) {
            this.setData({
                secondaryList: res.data.records,
            });
        }
    },
    // 点击二级标题获取二级标题下的数据  
    async secondaryPostText() {
        let res = await request({
            url: "Goods/findUserAll",
            data: this.secondary,
        });
        console.log(res, "点击二级分类的商品");

        // 获取 总条数
        const total = res.data.total;
        // 计算总页数
        this.totalPages = Math.ceil(total / this.secondary.size);
        if (res.code == 200) {
            this.setData({
                secondaryList: [...this.data.secondaryList, ...res.data.records],
            });
        }
    },

    // 销量
    pitchTap(e) {
        console.log(e);
        this.setData({
            pitchId: e.target.dataset.pitchid,
        });
        if (this.data.pitchId == "1") {
            console.log("等于1");
            this.setData({
                condition_one: true,
                condition_two: false,
            });
            this.collect();
        } else if (this.data.pitchId == "2") {
            console.log("等于2");
            this.setData({
                condition_one: false,
                condition_two: true,
            });
            this.fluctuation();
        }
    },
    // 销量上下按钮
    async collect(e) {
        console.log(e);
        var that = this;
        var isFavorite = that.data.isFavorite;
        //上
        if (isFavorite == false) {
            that.setData({
                isFavorite: true,
            });
            this.secondary.salesOrderType = 2;
            this.secondary.priceOrderType = "";
            this.secondary.page = 1;
            let res = await request({
                url: "Goods/findUserAll",
                data: this.secondary,
            });
            console.log(res, "上");
            if (res.code == 200) {
                this.setData({
                    secondaryList: res.data.records,
                });
            }
        }
        //下
        else if (isFavorite == true) {
            that.setData({
                isFavorite: false,
            });
            this.secondary.salesOrderType = 1;
            this.secondary.priceOrderType = "";
            this.secondary.page = 1;
            let res = await request({
                url: "Goods/findUserAll",
                data: this.secondary,
            });
            console.log(res, "上");
            if (res.code == 200) {
                this.setData({
                    secondaryList: res.data.records,
                });
            }
        }
    },
    // 价格上下按钮
    async fluctuation(e) {
        console.log(e);
        var that = this;
        var isPrice = that.data.isPrice;
        //上
        if (isPrice == false) {
            that.setData({
                isPrice: true,
            });
            this.secondary.salesOrderType = "";
            this.secondary.priceOrderType = 1;
            this.secondary.page = 1;

            let res = await request({
                url: "Goods/findUserAll",
                data: this.secondary,
            });
            console.log(res, "上");
            if (res.code == 200) {
                this.setData({
                    secondaryList: res.data.records,
                });
            }
        }
        //下
        else if (isPrice == true) {
            that.setData({
                isPrice: false,
            });

            this.secondary.salesOrderType = "";
            this.secondary.priceOrderType = 2;
            this.secondary.page = 1;

            let res = await request({
                url: "Goods/findUserAll",
                data: this.secondary,
            });
            console.log(res, "上");
            if (res.code == 200) {
                this.setData({
                    secondaryList: res.data.records,
                });
            }
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},
    //点击当前tab的函数
    onTabItemTap() {
        // this.setData({
        //     currentIndex: 0,
        // });
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        //  1 判断还有没有下一页数据
        console.log("88888888888888888");

        if (this.secondary.page >= this.totalPages) {
            wx.showToast({
                title: "我是有底线的",
                icon: "none",
            });
        } else {
            this.secondary.page++;
            console.log("888888888");

            if (this.secondary.classifyTwoId) {
                this.secondaryPostText(); //二级标题下的数据
                console.log("99999999");

            } else {
                this.primaryPostText(); //一级分类的下的所有商品数据
                console.log("77777777");

            }
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});